#!/bin/bash

#=======================================================================
# annex-cmd
# File ID: f1ca60b4-d33a-11e2-a3c0-0800274013ad
# Execute command in all local git-annex repositories
# License: GNU General Public License version 2 or later.
#=======================================================================

if [ "$1" = "--gadu" ]; then
    git update-dirs ~/annex /media/*/annex \
        -e "ga sync; sess ./lag_gadu-local *; git add .gadu-local; git ci -av; ga sync"
    exit
fi

wildc='*'
if [ "$1" = "--annex" ]; then
    unset wildc
    shift
fi
if [ "$1" = "-D" ]; then
    test -z "$2" && { echo annex-cmd: Missing argument for -D >&2; exit 1; }
    wildc="$2"
    shift 2
fi

if [ "$1" = "--count-loose" ]; then
    git update-dirs /media/*/annex/$wildc $HOME/annex/$wildc \
        -qq \
        -E 'pwd >&2' \
        -e 'echo $(find .git/objects -type f | grep -v -e pack -e info | wc -l) $(pwd)' \
        | sort -n
    exit
fi

if [ "$1" = "--find-extra" ]; then
    git update-dirs ~/annex/$wildc \
        -q \
        -E 'pwd >&2' \
        -e 'git update-dirs -Fg >/dev/null; ga whereis --copies=semitrusted+:$(echo $(ga numcopies) + 1 | bc)'
    exit
fi

git update-dirs /media/*/annex/$wildc $HOME/annex/$wildc "$@"
